
#include "Monster.h"




//simple absolute value function so we dont need to load all of <cmath> just for it
s32 abs(s32 v)
{
	if(v<0)
		v=0-v;
	return v;
}


Enemy::Enemy() 
{ 
	ret = false;
	damage = 10; 
	hp = 100;
	speed = 1;
	x = 10;
	y = 10;
	wand = 0;
	left = true;
	hit = false;
	award = false;
}

Enemy::Enemy(int type, int sx, int sy) // type of monster, x spawn coord, y spawn
{
	ret = false;
	counter = 0;
	wand = 0;
	left = true;
	hit = false;
	x = sx;
	y = sy;
	award = false;
	
	switch(type)
	{
		// Slime
		case 1:
			damage = 10; 
			hp = 100;
			speed = 1;
			
			break;
			
		//Beatle
		case 2:
			damage = 20; 
			hp = 50;
			speed = 2;

			break;
			
		//Boss
		case 3:
			damage = 10; 
			hp = 1000;
			speed = 5;
			
			break;
	
	}


}

bool Enemy::attack(int sprNo)
{
	PA_StartSpriteAnimEx(0, sprNo, 29, 31, 7, 1, 1); //attack animation
	if(PA_GetSpriteAnimFrame(0, sprNo) == 31)
	{
		ret = true; // start retreating after attack
		return true;	
	}
	else 
		return false;
		
	
	
	//PA_StartSpriteAnim(0, sprNo, 29, 31, 7);
	
	// stops all moving, start attacking animation
	
	//try to get the attacks in time with the animation
	
	// sends damage to the avatar class
}

void Enemy::wander(int sprNo) //   oldX/Y. scrolloffsetX/Y
{	
	//simple wandering back and forth 
	if (left == true)
	{
		wand++;
		
		if (wand >= 80)
		{
			PA_StartSpriteAnim(0, sprNo, 8, 13, 7); //walking left
			left = false;
		}
		else if (wand >= 40) // wait 40 cycles before moving again
		{
			x--;
		}

	}
	else if (left == false)
	{		
		wand--;
		
		if (wand <= 0)
		{
			left = true;
			PA_SetSpriteHflip(0, sprNo, 1); //flip
			PA_StartSpriteAnim(0, sprNo, 6, 7, 5); // walking right
			PA_SetSpriteHflip(0, sprNo, 0);
			
		}
		else if (wand <= 40)
		{
			x++;
		}

	}
	
	
}


void Enemy::gethurt(int d)
{
	// Damage from avatar send to monster's hp
	hp -= d;
}

bool Enemy::checkdead()
{
	if (hp <= 0)
		return false;
	else
		return true;
}



void Enemy::pursue() // input players position, basic pathfinding
{
	//u16 topLim = 40;
	//topLim += rand()%5; //add variance
	//u16 lowLim = 30;
	//lowLim += rand()%5; //add variance
	
	//PA_StartSpriteAnim(0, 40, 8, 13, 7);
	
	if (x>px+90)//70) // if monster is to the right
	{
		x--;
		//PA_SetSpriteHflip(0, 40, 1); //flip
		//PA_StartSpriteAnim(0, 40, 6, 7, 5); // walking right
		//PA_SetSpriteHflip(0, 40, 0);
	}
	else if (x<px+5)//15)  // if monster is to the left
	{
		x++;
		//PA_StartSpriteAnim(0, 40, 8, 13, 7); //walking left

	}
		
		
	if (y>py+40) // if monster is above
	{
		y--;
	}
	else if (y<py+40) // if monster is below
	{
		y++;
	}	
}


void Enemy::retreat(int r) 
{
	counter++;
	
	if (x>px+20) // if on the right
		x++;
	else // on the left
		x--;

	if (counter < r)
		ret = true;
	else
		ret = false;
		
}



void Enemy::updatePosition(int sprNo, s32 oldx, s32 oldy, s32 scrollx, s32 scrolly, s32 xcord, s32 ycord)
{
	//updates all coords every cycle 
	ox = oldx;
	oy = oldy;
	px = xcord;
	py = ycord;
	sosx = scrollx;
	sosy = scrolly;
	
	if(!(MonsterCheckCol(50, 60)))
	{

		PA_SetSpriteX(0, sprNo, x - ox - sosx);
		PA_SetSpriteY(0, sprNo, y - oy - sosy);
	}
}

bool Enemy::aggro()
{
	if ( px >= x-150 && px <= x+150 && py >= y-100 && py <= y+100 )  //box around monster for aggro radius
		return true;
	else
		return false;

}

bool Enemy::attackRange()
{
	if((y <= py + 45) && (y >= py + 35) && (x<=px+90) && (x>=px+5))
		return true;
	else 
		return false;
}


bool Enemy::invisible()
{
	if ( px >= x-300 && px <= x+300 && py >= y-280 && py <= y+280 )  //box around monster for visible radius
		return false;
	else
		return true;

}

u8 Enemy::MonsterCheckCol(u8 Cx, u8 Cy) //check x, check y
{
	u16 i = PA_EasyBgGetPixelCol(0,2,PA_GetSpriteX(0,0)+Cx,PA_GetSpriteY(0,0)+Cy);

	if (i==black)
		return 1;
	else
		return 0;
}





